package cn.innosmart.aq.manager;

import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ResponseHandler;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

import cn.innosmart.aq.camera.models.CameraBean;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/6/30.
 */
public class CameraManager extends BaseManager {
    private static RegisterCallBack registerCallBack;
    private static RemoveCallBack removeCallBack;
    private static UpdateCallBack updateCallBack;
    //目前没有摄像头 2015/6/30
    //加入摄像头 2015/10/13
    private String Tag = "CameraManager";
    /**
     * 添加摄像头回调函数
     */
    private ResponseHandler.ResponseCallback mCameraManagerRegisterCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "CameraManager.Register get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    String id = jRoot.getJSONObject("result").getString("id");
                    if (registerCallBack != null) {
                        registerCallBack.onRegisterResponseCallBack(0, id);
                    }
                } else if (-100 == rspCode) {
                    if (registerCallBack != null) {
                        registerCallBack.onRegisterResponseCallBack(-1, null);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    /**
     * 删除摄像头回调函数
     */
    private ResponseHandler.ResponseCallback mCameraManagerRemoveCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "CameraManager.Remove get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    if (removeCallBack != null) {
                        removeCallBack.onRemoveResponseCallBack(0);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    /**
     * List摄像头回掉函数
     */
    private ResponseHandler.ResponseCallback mCameraManagerListCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "CameraManager.List get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    JSONArray jsonArray = jRoot.getJSONObject("result").getJSONArray("cameras");
                    CameraBean cameraBean;
                    if (SystemConstant.cameraBeanHashMap == null) {
                        SystemConstant.cameraBeanHashMap = new HashMap<String, CameraBean>();
                    } else {
                        SystemConstant.cameraBeanHashMap.clear();
                    }
                    //初始化摄像头内容
                    for (int i = 0; i < jsonArray.length(); i++) {
                        cameraBean = new CameraBean(jsonArray.getJSONObject(i));
                        SystemConstant.cameraBeanHashMap.put(cameraBean.getUid(), cameraBean);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    /**
     * 更新摄像头回调函数
     */
    private ResponseHandler.ResponseCallback mCameraManagerUpdateCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "CameraManager.Update get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    if (updateCallBack != null) {
                        updateCallBack.onUpdateResponseCallBack(0);
                    }
                } else {
                    if (updateCallBack != null) {
                        updateCallBack.onUpdateResponseCallBack(-1);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (updateCallBack != null) {
                    updateCallBack.onUpdateResponseCallBack(-1);
                }
            }
        }
    };


    private CameraManager() {
    }

    public static CameraManager getInstance() {
        return CameraManagerInstance.instance;
    }

    // CameraManager.Register

    /**
     * 添加摄像头
     *
     * @param connectionEntity
     * @param params
     * @param callBack
     * @return
     */
    public boolean Register(ConnectionEntity connectionEntity, String params, RegisterCallBack callBack) {
        log(Tag, "Register. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.registerCallBack = callBack;
        long callid = System.currentTimeMillis();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mCameraManagerRegisterCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "CameraManager.Register", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // CameraManager.Remove

    /**
     * 删除摄像头
     *
     * @param connectionEntity
     * @param params
     * @param callBack
     * @return
     */
    public boolean Remove(ConnectionEntity connectionEntity, String params, RemoveCallBack callBack) {
        log(Tag, "Remove. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.removeCallBack = callBack;
        long callid = System.currentTimeMillis();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mCameraManagerRemoveCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "CameraManager.Remove", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // CameraManager.List

    /**
     * List摄像头
     *
     * @param connectionEntity
     * @return
     */
    public boolean List(ConnectionEntity connectionEntity) {
        log(Tag, "List. UID = " + connectionEntity.getUid());
        long callid = System.currentTimeMillis();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mCameraManagerListCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "CameraManager.List", new JSONObject().toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // CameraManager.List

    /**
     * List摄像头
     *
     * @param connectionEntity
     * @param delay
     * @return
     */
    public boolean List(ConnectionEntity connectionEntity, int delay) {
        log(Tag, "List. UID = " + connectionEntity.getUid());
        long callid = System.currentTimeMillis();
        if (delay != 0) {
            callid = callid + delay;
        }
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mCameraManagerListCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "CameraManager.List", new JSONObject().toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // CameraManager.Update

    /**
     * 更新摄像头数据
     *
     * @param connectionEntity
     * @param params
     * @param callBack
     * @return
     */
    public boolean Update(ConnectionEntity connectionEntity, String params, UpdateCallBack callBack) {
        log(Tag, "Update. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.updateCallBack = callBack;
        long callid = System.currentTimeMillis();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mCameraManagerUpdateCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "CameraManager.Update", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }


    public interface RegisterCallBack {
        void onRegisterResponseCallBack(int ret, String cameraid);
    }

    public interface RemoveCallBack {
        void onRemoveResponseCallBack(int ret);
    }

    public interface UpdateCallBack {
        void onUpdateResponseCallBack(int ret);
    }

    private static class CameraManagerInstance {
        private static final CameraManager instance = new CameraManager();
    }

}
